<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员分组
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
			array('op'=>'list','title'=>'敏感词监控'),
			array('op'=>'wordslist','title'=>'敏感词词库'),
			array('op'=>'grouplist','title'=>'敏感词分组'),
);
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['keywords'])){
		$params['keywords like']='%'.trim($_RQ['keywords']).'%';
		$return['params']['keywords']=$_RQ['keywords'];
	}
	if(!empty($_RQ['groupid'])){
		$wordsids = pdo_getall('jy_scrm_message_words',array('merchid'=>$_SESSION['onemerchid'],'groupid'=>$_RQ['groupid']),array('id'),'id');
		$wordsids=array_keys($wordsids);
		$params['wordsid']=$wordsids;
		$return['params']['groupid']=$_RQ['groupid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_message_keylog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['usernum']=pdo_count('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'groupid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			$item['qunnum']=0;
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'msgid','title'=>'信息ID','type'=>'string'),
			array('name'=>'keywords','title'=>'触发关键词','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'keywords','width'=>15,'title'=>'关键词','type'=>'string'),
			array('name'=>'groupid','width'=>15,'title'=>'分组','type'=>'select','options'=>'groups'),
		)
	);
	$return['groups'] = pdo_getall('jy_scrm_message_words_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='wordslist'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['keywords'])){
		$params['keywords like']='%'.trim($_RQ['keywords']).'%';
		$return['params']['keywords']=$_RQ['keywords'];
	}
	if(!empty($_RQ['groupid'])){
		$wordsids = pdo_getall('jy_scrm_message_words',array('merchid'=>$_SESSION['onemerchid'],'groupid'=>$_RQ['groupid']),array('id'),'id');
		$wordsids=array_keys($wordsids);
		$params['wordsid']=$wordsids;
		$return['params']['groupid']=$_RQ['groupid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_message_words',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['workernum']=0;
			$item['usernum']=0;
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','del_op'=>'worddel','del_one'=>true,'delall'=>true,'list'=>array(
			//array('name'=>'title','title'=>'敏感词名称','type'=>'string'),
			array('name'=>'words','title'=>'触发关键词','type'=>'string'),
			array('name'=>'workernum','title'=>'员工触发次数','type'=>'string'),
			array('name'=>'usernum','title'=>'客户触发次数','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'string'),
		)),
		'search_one'=>array('name'=>'title','add_op'=>'addword'),
	);
	$return['groups'] = pdo_getall('jy_scrm_message_words_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='addword'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(!$postdata['words']){
			exi('请填写关键词',true);
		}
		$data=array(
			'words'=>trim($postdata['words']),
			'groupid'=>trim($postdata['groupid']),
		);
		$id=intval($postdata['id']);
		if($id){
			pdo_update('jy_scrm_message_words',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$_SESSION['onemerchid'];
			pdo_insert('jy_scrm_message_words',$data);
		}
		exi('操作成功！','','wordslist');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_message_words',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'wordslist','title'=>'返回词库列表'),
		),
		'edit'=>array(
			array('type'=>'select','title'=>'选择分组','name'=>'groupid','options'=>'groups'),
			array('type'=>'text','title'=>'关键词','name'=>'words','help'=>'多个关键词用|隔开！'),
		)
	);
	$return['groups'] = pdo_getall('jy_scrm_message_words_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='grouplist'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['keywords'])){
		$params['keywords like']='%'.trim($_RQ['keywords']).'%';
		$return['params']['keywords']=$_RQ['keywords'];
	}
	if(!empty($_RQ['groupid'])){
		$wordsids = pdo_getall('jy_scrm_message_words',array('merchid'=>$_SESSION['onemerchid'],'groupid'=>$_RQ['groupid']),array('id'),'id');
		$wordsids=array_keys($wordsids);
		$params['wordsid']=$wordsids;
		$return['params']['groupid']=$_RQ['groupid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_message_words',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['workernum']=0;
			$item['usernum']=0;
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','del_op'=>'worddel','del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'title','title'=>'分组名称','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'title','add_op'=>'addgroup'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='addgroup'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(!$postdata['title']){
			exi('请填写标题',true);
		}
		$data=array(
			'title'=>trim($postdata['title']),
			'status'=>trim($postdata['status']),
		);
		$id=intval($postdata['id']);
		if($id){
			pdo_update('jy_scrm_message_words_group',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$_SESSION['onemerchid'];
			pdo_insert('jy_scrm_message_words_group',$data);
		}
		exi('操作成功！','','wordslist');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_message_words_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'grouplist','title'=>'返回分组列表'),
		),
		'edit'=>array(
			array('type'=>'string','title'=>'名称','name'=>'title'),
			array('type'=>'switch','title'=>'状态','name'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择用户组！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_scrm_message_words_group',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='worddel'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_message_words',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='groupdel'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_message_words_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}